const app=getApp();
Page({
  data: {
      
      yzmCode:'',
    phone: '',
    num:'',
    smsCode: '',
    password: '',
    password1: '',
    smsCodeButtonText: '获取验证码',
    smsCodeButtonDisabled: false,
    timer: null,
    countdown: 60
  },

  onPhoneInput(event) {
    this.setData({
      phone: event.detail
    });
  },

  onSmsCodeInput(event) {
    this.setData({
      smsCode: event.detail
    });
  },

  onPasswordInput(event) {
    this.setData({
      password: event.detail
    });
  },

  onPasswordInput1(event) {
    this.setData({
      password1: event.detail
    });
  },

  onGetSmsCode() {
    const { phone } = this.data;

    if (!phone) {
      wx.showToast({
        title: '请输入手机号',
        icon: 'none'
      });
      return;
    }else{
        let url=app.globalData.URL+'/users/yzmCode';
        let data={
            phone:phone
        }
        app.wxRequest('GET',url,data,(res)=>{
              this.setData({
                  yzmCode:res.data
              })
        })
    }

    this.setData({
      smsCodeButtonDisabled: true,
      smsCodeButtonText: `${this.data.countdown}秒`
    });

    this.startCountdown();

    // 在此处调用后台 API 获取验证码
   
  },

  startCountdown() {
    const timer = setInterval(() => {
      let countdown = this.data.countdown - 1;
      if (countdown === 0) {
        clearInterval(this.data.timer);
        this.setData({
          smsCodeButtonDisabled: false,
          smsCodeButtonText: '获取验证码',
          countdown: 60
        });
      } else {
        this.setData({
          countdown: countdown,
          smsCodeButtonText: `${countdown}秒`
        });
      }
    }, 1000);

    this.setData({ timer });
  },
  onLoad(options){
      this.setData({
        num:options.num
        
      })
      console.log(this.data.num)


  },
  panduan(){
   
    var reg = /^1[3-9]\d{9}$/
    if(!reg.test(this.data.phone)){
        wx.showToast({
          title: '手机号无效',
        })

    }
  },

  onRegister() {
    const { phone, smsCode, password, password1 } = this.data;
    console.log(smsCode)
    console.log(this.data.yzmCode)

    if (!phone || !smsCode ) {
      wx.showToast({
        title: '请填写完整信息',
        icon: 'none'
      });
      return;
    }

    if (this.data.smsCode != this.data.yzmCode) {
      wx.showToast({
        title: '验证码不正确',
        icon: 'none'
      });
      return;
    }

    // 在此处执行注册逻辑，例如调用后端 API
    wx.showToast({
      title: '验证成功',
      icon: 'success'
    });
    wx.redirectTo({
      url: '/pages/xgym/xgym?phone='+this.data.phone+'&num='+this.data.num,
    })
  }
});
